
package com.idea.rdftree;

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.idea.common.swing.treetable.DynamicTreeTableModel;
import com.idea.common.swing.treetable.TreeTableModel;

//public class RdfTreeModel extends DefaultTreeModel implements TreeTableModel {
public class RdfTreeModel
	extends DynamicTreeTableModel
	implements TreeTableModel {
	private RdfNodeMap nodeMap;
	private Model model;

	private DefaultTreeModel defaultTreeModel;

	/**
		* Names of the columns.
		*/
	private static final String[] columnNames =
		{ "Node", "Item", "Value"};
	/**
		* Method names used to access the data to display.
		*/
	private static final String[] methodNames =
		{ "getName", "getNodeTypeName" ,  "getRdfValue" };
	/**
		* Method names used to set the data.
		*/
	private static final String[] setterMethodNames =
		{ "setName", "setLocation", "setRdfValue" };
	/**
		* Classes presenting the data.
		*/
	private static final Class[] classes =
		{ TreeTableModel.class, String.class, String.class };

	public RdfTreeModel(TreeNode root, Model model, RdfNodeMap nodeMap) {
		super(root, columnNames, methodNames, setterMethodNames, classes);
		//  super();
		setModel(model);
		this.nodeMap = nodeMap;
		nodeMap.setTreeModel(this);
		defaultTreeModel = new DefaultTreeModel(root); // added@@
	}
	
	/**
		 * Returns the column name passed into the constructor.
		 */
	
		public String getColumnName(int column) {
			System.out.println("get co.umn name "+column+columnNames[column]);
			return columnNames[column];
		}


	public TreeNode getTreeNode(RDFNode rdfNode) {
		return nodeMap.getTreeNode(rdfNode);
	}

	public Object getRoot() {
		//	System.out.println("ROOT = " + nodeMap.getRoot());
		//System.out.println("ROOTsize = " + nodeMap.getRoot().getChildCount());
		return nodeMap.getRoot();
	}

	public boolean isLeaf(Object node) {
	//	System.out.println(  ((RdfTreeNode) node).isLeaf());
		return ((RdfTreeNode) node).isLeaf();
		//return false;
	}
	
	// was commented
	    public int getIndexOfChild(Object parent, Object child) {
	        return ((RdfTreeNode)parent).getIndex((RdfTreeNode)child);
	    }
	

	public Model getModel() {
		return model;
	}

	/**
	 * Sets the model.
	 * @param model The model to set
	 */
	public void setModel(Model model) {
		this.model = model;
	}

	public void reload() {
		nodeMap.reload();
		//  super.reload();
	}

	////////////////// TreeTable Methods //////////////
	/*
	public int getColumnCount() {
		return 2;
	}
	
	public String getColumnName(int column) {
		return "column" + column;
	}
	*/
	// ?????
	/*
	static protected Class[] cTypes =
		{ TreeTableModel.class, Integer.class, String.class, Date.class };
	
	public Class getColumnClass(int column) {
		//	return java.lang.String.class;
		return cTypes[column];
	}
	*/
	
	/*
	public Object getValueAt(Object node, int column) {
		switch (column) {
			case 1 :
				return getRdfValueLabel(node);
			case 2 :
				return getRdfTypeLabel(node);
			default :
				return super.getValueAt(node, column);
		}
	}
	*/
	

	public String getRdfValueLabel(Object node) {
		return ((RdfTreeNode) node).getNodeTypeName();
	}

	public String getRdfTypeLabel(Object node) {
		return ((RdfTreeNode) node).getRdfValue();
	}


	public boolean isCellEditable(Object node, int column) {
		if(column == 0){
			return true;
		}
		return (column == 2)&&((RdfTreeNode)node).isValueEditable();
	}
	
	
	public void setValueAt(Object value, Object node, int column) {
		if(column == 0){
			return;
		}
		((RdfTreeNode)node).setRdfValue(value.toString());
	}

		/*
	public void setRdfTreeNode(RdfTreeNode rdfTreeNode) {
			this.rdfTreeNode = rdfTreeNode;
			nodeTypeLabel.setText(rdfTreeNode.getNodeTypeName());
			nodeValueField.setText(rdfTreeNode.getRdfValue());
	
					nodeValueField.setEditable(rdfTreeNode.isTextEditable());
					if(rdfTreeNode.isTextEditable()){
						nodeValueField.setBackground(RdfEditor.READ_WRITE_COLOR);
					}else{
						nodeValueField.setBackground(RdfEditor.READ_ONLY_COLOR);
					}
		}
		*/

	//	public boolean isCellEditable(Object node, int column) {
	//		return false;
	//	}

	//	public void setValueAt(Object aValue, Object node, int column) {
	//	}

/*
	public Object getChild(Object parent, int index) { // added @@
		return defaultTreeModel.getChild(parent, index);
	}

	public int getChildCount(Object parent) {
		return defaultTreeModel.getChildCount(parent);
	}
*/
		// -------------------
}
